﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Linq;

using OctoOS;
using OctoOS.UI;

public partial class _default : OOSPage
{
    private Resource _resource = null;

   protected void Page_Load(object sender, EventArgs e)
   {
       PageTitle = "Classifieds";

       _resource = DataSource.ClassifiedResources_Get(
           OOSRequest.Current.ResourceID);
       SetActions();
       if (_resource != null)
       {
           Helper.Categories.UpdateCategoryCache();

           Category[] allCategories = Helper.Categories.GetAllCategories();

           foreach (var cat in allCategories.Where(f => f.ParentCategoryID == 0))
           {
               TextTreeNode rootNode = new TextTreeNode(cat.Name);
               rootNode.HRef = cat.RedirectUrl == "" ? Urls.Classifieds.GetCategoryInfo(cat.ID) : cat.RedirectUrl;
               rootNode.Icon = cat.ImageUrl; 
               foreach (var childCat in allCategories.Where(f => f.ParentCategoryID == cat.ID))
               {
                   TextTreeNode childNode = new TextTreeNode(childCat.Name);
                   childNode.Icon = childCat.ImageUrl; 
                   childNode.HRef = childCat.RedirectUrl == "" ? Urls.Classifieds.GetCategoryInfo(childCat.ID) : childCat.RedirectUrl;
                   foreach (var grandChildCat in allCategories.Where(f => f.ParentCategoryID == childCat.ID))
                   {
                       TextTreeNode grandChildNode = new TextTreeNode(grandChildCat.Name);
                       grandChildNode.Icon = grandChildCat.ImageUrl; 
                       grandChildNode.HRef = grandChildCat.RedirectUrl == "" ? Urls.Classifieds.GetCategoryInfo(grandChildCat.ID) : grandChildCat.RedirectUrl;
                       childNode.Nodes.Add(grandChildNode);
                   }
                   rootNode.Nodes.Add(childNode);
               }
               categoryTree.Nodes.Add(rootNode);
           }
       }
   }

   private void SetActions()
   {
       if (_resource != null)
       {
           //AddAction("Samples", "/samples/default.aspx");
           if (_resource.AllowAnonAds)
           {
               AddAction("Add New Ad", Urls.Classifieds.GetSelect(0, 0));
           }
           else
           {
               if (OOSRequest.Current.UserID != 0)
               {
                   AddAction("Add New Ad", Urls.Classifieds.GetSelect(0, 0));
               }
           }

           if (OOSRequest.Current.UserID != 0)
           {
               AddAction("My Ads", Urls.Classifieds.GetMyAds((byte)Helper.AdType.Offer, (int)Helper.AdStatus.Active));
           }
       }
#warning admin 
       //if (OOSRequest.Current.IsModerator)
       {
           AddAction("Admin Page", Urls.Classifieds.GetAdminDefault());
       }

   }
}